<template>
  <div>
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="用户">
          <el-input v-model="formInline.user" placeholder="用户"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
          <el-button type="primary" @click="addUser">添加角色</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="id" width=""> </el-table-column>
      <el-table-column prop="userName" label="用户名"> </el-table-column>
      <el-table-column prop="name" label="姓名" width=""> </el-table-column>
      <el-table-column prop="createdAt" label="注册时间"> 
        <template slot-scope="scope">
            <span>{{scope.row.createdAt.split(' ')[0]}}</span>
        </template>

      </el-table-column>
      <el-table-column prop="lastLoginAt" label="最后登录时间">
         <template slot-scope="scope">
            <span>{{scope.row.lastLoginAt}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="role" label="角色">
        <template slot-scope="scope">
          <span v-if="scope.row.role == 1">超级管理员</span>
          <span v-if="scope.row.role == 2">管理员</span>
          <span v-if="scope.row.role == 3">普通用户</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="100"
        layout="total, sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //头部表单
      formInline: {
        user: "",
      },
      total: 0, //数据总条数
      tableData: null, //表格数据
      pageNumber: 1,
      pageSize: 10,
      //控制请求的列表的状态
      state: "初始化",
    };
  },
  methods: {
    //删除角色
    handleDelete(index, content) {
      // console.log(content,'content')
      this.$axios({
        method: "delete",
        url: "/admin",
        data: {
          id: content.id,
        },
      }).then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          this.getData();
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "error",
          });
        }
      });
    },
    //编辑角色
    handleEdit(index, content) {
      console.log(content)
      this.$router.push({
        path:"/home/adduser",
        query:{
          tag:"编辑",
          data:content
        }
      })
    },
    //获取搜索数据方法
    getSearchData() {
      this.$axios
        .get("/admin", {
          params: {
            userName: this.formInline.user,
            page: this.pageNumber,
            rows: this.pageSize,
          },
        })
        .then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            this.tableData = res.data.data.list;
            this.total = res.data.data.count;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "error",
            });
          }
        });
    },
    //点击搜索
    search() {
      this.state = "搜索";
      this.getSearchData();
    },
    //跳转添加用户页面
    addUser() {
      this.$router.push("/home/addUser");
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;

      if (this.state == "初始化") {
        this.getData();
      }
      if (this.state == "搜索") {
        this.getSearchData();
      }
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNumber = val;
      // console.log(this.state);
      if (this.state == "初始化") {
        this.getData();
      }
      if (this.state == "搜索") {
        this.getSearchData();
      }
    },
    //初始化列表方法
    getData() {
      this.$axios
        .get("/admin", {
          params: { page: this.pageNumber, rows: this.pageSize },
        })
        .then((res) => {
          // console.log(res);
          if (res.data.code == 200) {
            this.tableData = res.data.data.list;
            this.total = res.data.data.count;
            // console.log(this.total);
            // console.log(this.tableData, "this.tableData");
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "error",
            });
          }
        });
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style>
</style>